import java.util.Arrays;

public class Test {

    // 选择排序
    public static void selectSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i+1; j < array.length; j++) {
                if(array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            swap(array,i,minIndex);
        }
    }

    private static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    // 堆排序
    public static void heapSort(int[] array){
        //建立大根堆——升序排列
        for (int parent = (array.length-1-1) / 2; parent >= 0; parent--) {
            siftDown(array,parent,array.length);
        }

        //开始排序
        int end = array.length - 1;
        while(end > 0) {
            swap(array,0,end);
            siftDown(array,0,end);
            end--;
        }
    }

    private static void siftDown(int[] array, int parent,int size) {
        int child = 2*parent + 1;
        while(child < size) {
            if(child+1 < size && array[child+1] > array[child]) {
                child++;
            }
            if(array[parent] < array[child]) {
                swap(array,parent,child);
                parent = child;
                child = 2*parent+1;
            } else {
                break;
            }
        }
    }

    // 冒泡排序
    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length-1; i++) {
            boolean flag = false;
            for (int j = 0; j < array.length - 1 - i; j++) {
                if(array[j] > array[j+1]) {
                    swap(array,j,j+1);
                    flag = true;
                }
            }
            if(!flag) {
                break;
            }
        }
    }

    // 快速排序
    public static void quickSort(int[] array){
         quick(array,0,array.length-1);
    }

    private static void quick(int[] array, int left, int right) {
        if(left >= right) {
            return;
        }

        int pivot = partition(array,left,right);
        quick(array,left,pivot - 1);
        quick(array,pivot+1,right);
    }

    private static int partition(int[] array, int left, int right) {
        //三数取中法
        getMidPivot(array,left,right);
        int key = array[left];

        while(left < right) {
            while(left < right && array[right] >= key) {
                right--;
            }
            array[left] = array[right];

            while(left < right && array[left] <= key) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = key;

        return left;
    }

    private static void getMidPivot(int[] array, int left, int right) {
        int mid = (left + right) / 2;
        if(array[left] < array[right]) {
            if(array[mid] < array[left]) {
                return;
            } else if(array[mid] > array[right]) {
                swap(array,left,right);
            } else {
                swap(array,mid,left);
            }
        } else {
            if(array[mid] < array[right]) {
                swap(array,left,right);
            } else if(array[mid] > array[left]) {
                return;
            } else {
                swap(array,mid,left);
            }
        }
    }

    public static void main(String[] args) {
        int[] arr = {1,9,2,5,12,0,4,6,8,10};
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));
    }
}
